<?php
final class DBConn extends Database
{
    private $query;
    private $binds = array();



    /**
     * Konstruktor
     * Erstellt eine MySQL-Verbindung mittels PDO und speichert den Link in $this->link
     *
     * @param String $host
     * @param String $username
     * @param String $password
     * @param String $database
     * @param Integer $port
     */
    public function __construct($host,$username,$password,$database,$port=3306)
    {
        try
        {
            $this->link = mysql_connect($host.':'.$port,$username,$password);
            if (!$this->link)
            {
                throw new Exception(mysql_error().' ('.mysql_errno().')');
            }
            
            if (!mysql_select_db($database,$this->link))
            {
                throw new Exception(mysql_error($this->link).' ('.mysql_errno($this->link).')');
            }
        }
        catch (Exception $e)
        {
            die($e->getMessage());
        }
        unset($host,$username,$port,$password,$database);
    }




    /**
     * Bereitet einen MySQL-Query fuer die Abfrage vor
     *
     * @param String $query
     */
    public function prepare($query)
    {
        $this->query = $query;
        unset($param,$query);
    }




    /**
     * Gibt alle Zeilen der Abfrage in einem Array zurueck
     *
     * @param String $type
     * @return Array
     */
    public function fetchAll($type = parent::FETCH_ASSOC)
    {
        try
        {
            switch ($type)
            {
                case parent::FETCH_BOTH:
                    $type = MYSQL_BOTH;
                    break;
                case parent::FETCH_NUM:
                    $type = MYSQL_NUM;
                    break;
                case parent::FETCH_ASSOC:
                default:
                    $type = MYSQL_ASSOC;
                    break; 
            }
            if ($this->prepared && is_resource($this->prepared))
	        {
	            $arr = array();
	            while($array = mysql_fetch_array($this->prepared,$type))
	            {
	                $arr[] = $array;
	            }
	            return $arr;	            
	        }
        }
        catch (Exception $e)
        {
            die($e->getMessage());
        }
        unset($type);
        return array();
    }




    /**
     * Schliesst den Cursor und setzt ihn auf NULL
     *
     */
    public function free()
    {
        if ($this->prepared && is_resource($this->prepared))
        {
            @mysql_free_result($this->prepared);
        }
        unset($this->prepared);
        $this->prepared = NULL;
    }




    /**
     * Bindet einen Wert an einen zuvor in $this->prepare vorbereiteten Query festgelegten Platzhalter der formatierung ":platzhalter"
     *
     * @param String $name
     * @param String $value
     * @param Integer $param
     */
    public function bind($name,$value,$param)
    {
        $this->binds[$name] = array('value' => $value,'param' => $param);
        unset($name,$value,$param);
    }
    
    
    
    
    
    private function escape($data,$param = parent::PARAM_STR)
    {
        switch ($param)
        {
            case parent::PARAM_INT:
                return intval($data);
                break;
            case parent::PARAM_NULL:
                return 'NULL';
                break;
            case parent::PARAM_BOOL:
                return intval((bool)$data);
                break;
            case parent::PARAM_STR:
            default:
                return '"'.mysql_real_escape_string($data,$this->link).'"';
                break;
        }
    }




    /**
     * Fuehrt die Abfrage aus
     *
     */
    public function execute()
    {
        try
        {
            $arr = array();
            foreach ($this->binds as $key => $value)
            {
                $pos = stripos($this->query,':'.$key);
                if ($pos !== FALSE)
                {
                     $arr[$pos] = $key;
                }
            }
            krsort($arr);
            foreach ($arr as $key => $value)
            {
                $this->query = substr_replace($this->query,$this->escape($this->binds[$value]['value'],$this->binds[$value]['param']),$key,strlen($this->binds[$value]['value']));
            }
            $this->prepared = mysql_query($this->query,$this->link);
            if (!$this->prepared)
            {
                throw new Exception(mysql_error($this->link).' ('.mysql_errno($this->link).')');
            }
        }
        catch (Exception $e)
        {
            die($e->getMessage());
        }
    }





    /**
     * Destrctor
     */
    public function __destruct()
    {
        if ($this->prepared && is_resource($this->prepared))
        {
            @mysql_free_result($this->prepared);
        }
        $this->link = NULL;
        unset($this->link);
    }





    /**
     * Liefert die ID, die zuletzt per auto-increment gesetzt wurde
     *
     * @return String
     */
    public function lastInsertId()
    {
        return(mysql_insert_id($this->link));
    }





}